const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/playground', { useNewUrlParser: true, useUnifiedTopology: true })
    .then(() => console.log('数据库连接成功'))
    .catch(err => console.log(err, '数据库连接失败'))

//用户集合规则
const userSchema = mongoose.Schema({
    name: {
        type: String,
        require: true
    }
});

//文章集合规则
const postSchema = mongoose.Schema({
    title: {
        type: String
    },
    author: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User'
    }
});
//用户集合
const User = mongoose.model('User', userSchema);
//文章集合
const Post = mongoose.model('Post', postSchema);

// User.create({
//     name: '邓建'
// }).then(result => console.log(result))

// Post.create({
//     title: '123',
//     author: '5fb7765561133810a8017b10'
// }).then(result => console.log(result))

Post.find().populate('author').then(result => console.log(result))